home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / apache / NcgiServer.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  2.7 KB  |  110 lines

  1. // NcgiServer - an NCGI server
  2. //
  3. // Copyright (c) 1996 Sun Microsystems, Inc.  All Rights reserved
  4. // Permission to use, copy, modify, and distribute this software
  5. // and its documentation for NON-COMMERCIAL purposes and without
  6. // fee is hereby granted provided that this copyright notice
  7. // appears in all copies. Please refer to the file copyright.html
  8. // for further important copyright and licensing information.
  9. //
  10. // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11. // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14. // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  
  17. package sun.servlet.apache;
  18.  
  19. import java.io.*;
  20. import java.net.*;
  21.  
  22. /** An NCGI server.
  23. */
  24.  
  25. public class NcgiServer
  26.     {
  27.  
  28.     private ServerSocket serverSocket;
  29.  
  30.     /** Create an NCGI server.
  31.     ** @exception NcgiException if there's a problem
  32.     */
  33.     public NcgiServer( int port, String authfile ) throws NcgiException
  34.     {
  35.     try
  36.         {
  37.         serverSocket = new ServerSocket( port, Integer.MAX_VALUE );
  38.         }
  39.     catch ( IOException e )
  40.         {
  41.         throw new NcgiException(
  42.         "problem creating server socket: " + e.toString() );
  43.         }
  44.     makeAuthfile( authfile );
  45.     }
  46.     
  47.  
  48.     public Socket accept() throws NcgiException
  49.     {
  50.     try
  51.         {
  52.         Socket socket = serverSocket.accept();
  53.         return socket;
  54.         }
  55.     catch ( IOException e )
  56.         {
  57.         throw new NcgiException(
  58.         "problem accepting new socket: " + e.toString() );
  59.         }
  60.     }
  61.  
  62.  
  63.     public void done()
  64.     {
  65.     nukeAuthfile();
  66.     try
  67.         {
  68.         serverSocket.close();
  69.         }
  70.     catch ( IOException e ) {}
  71.     }
  72.  
  73.  
  74.     private String authfile;
  75.     private File authFile;
  76.     protected byte[] authBytes = new byte[25]; // arbitrary size
  77.  
  78.     /** Initialize the authorization file.
  79.     ** @param authfile the name of the file with the authorization string
  80.     ** @exception NcgiException if there's a problem
  81.     */
  82.     private void makeAuthfile( String authfile ) throws NcgiException
  83.     {
  84.     this.authfile = authfile;
  85.     for ( int i = 0; i < authBytes.length; ++i )
  86.         authBytes[i] = (byte) ( Math.random() * 256.0 );
  87.     try
  88.         {
  89.         authFile = new File( authfile );
  90.         OutputStream out = new FileOutputStream( authFile );
  91.         out.write( authBytes );
  92.         out.close();
  93.         }
  94.     catch ( IOException e )
  95.         {
  96.         throw new NcgiException(
  97.         "problem making authfile: " + e.toString() );
  98.         }
  99.     }
  100.     
  101.     /** Get rid of the authorization file.  Can be called when your server
  102.     ** is exiting, if you're paranoid.
  103.     */
  104.     private void nukeAuthfile()
  105.     {
  106.     authFile.delete();
  107.     }
  108.  
  109.     }
  110.